package org.ycx.upms.service.impl;

import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.ycx.common.annotation.BaseService;
import org.ycx.common.base.BaseServiceImpl;
import org.ycx.common.db.DataSourceEnum;
import org.ycx.common.db.DynamicDataSource;
import org.ycx.upms.dao.mapper.*;
import org.ycx.upms.dao.model.*;
import org.ycx.upms.service.ISecUserService;

import java.util.ArrayList;
import java.util.List;

/**
 * SecUserService实现
 * Created by yang on 2017/6/26.
 */
@Service
@Transactional
@BaseService
public class SecUserServiceImpl extends BaseServiceImpl<ISecUserMapper, SecUser, SecUserExample> implements ISecUserService {

    private static Logger _log = LoggerFactory.getLogger(SecUserServiceImpl.class);

    @Autowired
    ISecUserMapper secUserMapper;

    @Autowired
    ISecUserRoleMapper userRoleMapper;

    @Autowired
    ISecGroupUserMapper groupUserMapper;

    @Autowired
    ISecGroupRoleMapper groupRoleMapper;

    @Autowired
    ISecGroupMapper groupMapper;

    @Override
    public SecUser findUserInfo(String loginName) {
        DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName());
        SecUserExample sue = new SecUserExample();
        sue.createCriteria().andLoginNameEqualTo(loginName);
        List<SecUser> users = this.secUserMapper.selectByExample(sue);
        if (users != null && users.size() > 0) {
            return users.get(0);
        } else {
            return null;
        }
    }

    @Override
    public List<SecUser> selectUserByRoleId(Integer rid, int offset, int limit) {
        try {
            DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName());
            PageHelper.startPage(offset, limit, false);
            return this.secUserMapper.selectUserByRoleId(rid);
        } catch (Exception var6) {
            DynamicDataSource.clearDataSource();
            return null;
        }
    }

    @Override
    public long countUserByRoleId(Integer rid) {
        try {
            DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName());
            return this.secUserMapper.countUserByRoleId(rid);
        } catch (Exception var6) {
            DynamicDataSource.clearDataSource();
            return 0;
        }
    }

    @Override
    public List<Integer> selectRidsByUser(Integer uid) {
        DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName());
        return this.mapper.selectRidByUid(uid);
    }

    @Override
    public int updateUserRole(Integer uid, Integer[] rids) {
        DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
        int res = 0;
        SecUserRoleExample spe = new SecUserRoleExample();
        spe.createCriteria().andUserIdEqualTo(uid);
        res += this.userRoleMapper.deleteByExample(spe);
        if (null != rids && rids.length > 0) {
            for (int i = 0; i < rids.length; i++) {
                SecUserRole srp = new SecUserRole();
                srp.setRoleId(rids[i]);
                srp.setUserId(uid);
                res += this.userRoleMapper.insert(srp);
            }
        }
        return res;
    }

    @Override
    public List<SecGroup> findUserGroups(SecGroupUserExample sgu) {
        DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName());
        List<SecGroupUser> sgus = this.groupUserMapper.selectByExample(sgu);
        List<Integer> gids = new ArrayList<Integer>();
        for (SecGroupUser ss : sgus) {
            gids.add(ss.getGroupId());
        }
        if (gids.size() == 0) {
            return new ArrayList<SecGroup>();
        } else {
            SecGroupExample sge = new SecGroupExample();
            sge.createCriteria().andGidIn(gids);
            return this.groupMapper.selectByExample(sge);
        }
    }

    @Override
    public List<Integer> findUserGid(Integer uid) {
        DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName());
        SecGroupUserExample sgu = new SecGroupUserExample();
        sgu.createCriteria().andUidEqualTo(uid);
        List<SecGroupUser> sgus = this.groupUserMapper.selectByExample(sgu);
        List<Integer> gids = new ArrayList<Integer>();
        for (SecGroupUser ss : sgus) {
            gids.add(ss.getGroupId());
        }
        return gids;
    }

    @Override
    public int updateUserGroups(Integer uid, Integer[] gids) {
        DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
        int res = 0;
        //更新用户group
        SecGroupUserExample sgu = new SecGroupUserExample();
        sgu.createCriteria().andUidEqualTo(uid);
        res += this.groupUserMapper.deleteByExample(sgu);
        List<Integer> groupIds = new ArrayList<Integer>();
        if (gids != null && gids.length > 0) {
            for (Integer gid : gids) {
                SecGroupUser secGroupUser = new SecGroupUser();
                secGroupUser.setUid(uid);
                secGroupUser.setGroupId(gid);
                res += this.groupUserMapper.insert(secGroupUser);
                groupIds.add(gid);
            }
        }
        //更新角色
        res += this.userRoleMapper.deleteByGroup(uid);
        SecGroupRoleExample sgre = new SecGroupRoleExample();
        sgre.createCriteria().andGroupIdIn(groupIds);
        List<SecGroupRole> roles = this.groupRoleMapper.selectByExample(sgre);
        if (gids != null && gids.length > 0) {
            for (SecGroupRole role : roles) {
                SecUserRoleExample sure = new SecUserRoleExample();
                sure.createCriteria().andUserIdEqualTo(uid).andRoleIdEqualTo(role.getRoleId());
                long count = this.userRoleMapper.countByExample(sure);
                if (count > 0) {
                    continue;
                } else {
                    res += this.userRoleMapper.insertGroupRole(role.getRoleId(), uid, role.getGroupId());
                }
            }
        }
        return res;
    }
}